package com.empire.module.member.controller.admin.superrelationnode;

import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import java.util.*;
import java.io.IOException;

import com.empire.framework.common.pojo.PageParam;
import com.empire.framework.common.pojo.PageResult;
import com.empire.framework.common.pojo.CommonResult;
import com.empire.framework.common.util.object.BeanUtils;
import static com.empire.framework.common.pojo.CommonResult.success;

import com.empire.framework.excel.core.util.ExcelUtils;

import com.empire.framework.apilog.core.annotation.ApiAccessLog;
import static com.empire.framework.apilog.core.enums.OperateTypeEnum.*;

import com.empire.module.member.controller.admin.superrelationnode.vo.*;
import com.empire.module.member.dal.dataobject.superrelationnode.SuperRelationNodeDO;
import com.empire.module.member.service.superrelationnode.SuperRelationNodeService;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

@Tag(name = "管理后台 - 超级关系--节点信息")
@RestController
@RequestMapping("/member/super-relation-node")
@Validated
public class SuperRelationNodeController {

    @Resource
    private SuperRelationNodeService superRelationNodeService;

    @PostMapping("/create")
    @Operation(summary = "创建超级关系--节点信息")
    @PreAuthorize("@ss.hasPermission('member:super-relation-node:create')")
    public CommonResult<Long> createSuperRelationNode(@Valid @RequestBody SuperRelationNodeSaveReqVO createReqVO) {
        return success(superRelationNodeService.createSuperRelationNode(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新超级关系--节点信息")
    @PreAuthorize("@ss.hasPermission('member:super-relation-node:update')")
    public CommonResult<Boolean> updateSuperRelationNode(@Valid @RequestBody SuperRelationNodeSaveReqVO updateReqVO) {
        superRelationNodeService.updateSuperRelationNode(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除超级关系--节点信息")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('member:super-relation-node:delete')")
    public CommonResult<Boolean> deleteSuperRelationNode(@RequestParam("id") Long id) {
        superRelationNodeService.deleteSuperRelationNode(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得超级关系--节点信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('member:super-relation-node:query')")
    public CommonResult<SuperRelationNodeRespVO> getSuperRelationNode(@RequestParam("id") Long id) {
        SuperRelationNodeDO superRelationNode = superRelationNodeService.getSuperRelationNode(id);
        return success(BeanUtils.toBean(superRelationNode, SuperRelationNodeRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得超级关系--节点信息分页")
    @PreAuthorize("@ss.hasPermission('member:super-relation-node:query')")
    public CommonResult<PageResult<SuperRelationNodeRespVO>> getSuperRelationNodePage(@Valid SuperRelationNodePageReqVO pageReqVO) {
        PageResult<SuperRelationNodeDO> pageResult = superRelationNodeService.getSuperRelationNodePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, SuperRelationNodeRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出超级关系--节点信息 Excel")
    @PreAuthorize("@ss.hasPermission('member:super-relation-node:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportSuperRelationNodeExcel(@Valid SuperRelationNodePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<SuperRelationNodeDO> list = superRelationNodeService.getSuperRelationNodePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "超级关系--节点信息.xls", "数据", SuperRelationNodeRespVO.class,
                        BeanUtils.toBean(list, SuperRelationNodeRespVO.class));
    }

}